﻿@using A2.ERP.Models
@{
    List<t_UserTypeSubClass> userTypeSubList = ViewBag.userTypeSubClass;
    List<t_UserType> userType = ViewBag.userType;
}

<section id="SysUserTypeManager">
    <div class="card">
        <div class="card-header">添加群组</div>
        <div class="card-body">
            @using (Html.BeginForm("SysUserTypeManager", "Setting", FormMethod.Post, new
            {
                @class = "form addUserType"
            }))
            {
                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-append">
                            <span class="input-group-text">
                                上级类型
                            </span>
                        </div>
                        <select class="custom-select" name="UserTypeID" id="UserType">
                            <option selected>Choose...</option>
                            @foreach (var item in userType)
                            {
                                <option value="@item.TypeID">@item.TypeName</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label>群组名称</label>
                    <input type="text" name="SubClassName" value="" class="form-control" />
                </div>
                <div class="form-group">
                    <label>状态</label>
                    <div>
                        <input type="radio" name="state" value="Y" checked />
                        启用
                        <input type="radio" name="state" value="N" />
                        禁用
                    </div>
                </div>
                <div class="form-group">
                    <button class="btn btn-primary">添加</button>
                </div>
            }
        </div>
    </div>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>上级类型</th>
                <th>群名</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var userTypeSub in userTypeSubList)
            {
                <tr>
                    <td>@userTypeSub.t_UserType.TypeName</td>
                    <td>@userTypeSub.SubClassName</td>
                    <td>
                        @Html.Raw(userTypeSub.State == "Y"
                       ? @"<i class='icon SkyUI icon-check-circle text-success'></i>"
                        : @"<i class='icon SkyUI icon-close-circle text-danger'></i>")
                    </td>
                    <td>
                        <button class="btn btn-sm btn-primary btn-editUserType" data-target="#updateUserTypeBrandModal" data-toggle="modal" data-id="@userTypeSub.SubClassID">
                            <i class="icon SkyUI icon-search"></i>
                            编辑
                        </button>
                        <button class="btn btn-sm btn-danger btn-delUserType" data-target="#delUserTypeModal" data-toggle="modal" data-id="@userTypeSub.SubClassID">
                            <i class="icon SkyUI icon-close"></i>
                            删除
                        </button>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <div class="modal fade" id="delUserTypeModal" tabindex="-1" role="dialog" aria-labelledby="sysUserModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">确定要删除该权限组吗？</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    确定要删除该权限组吗?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-sureDelUserType">确定</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="updateUserTypeBrandModal" tabindex="-1" role="dialog" aria-labelledby="updateUserTypeModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">修改用户类型</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    @using (Html.BeginForm("EditUserType", "Setting", FormMethod.Post, new
                    {
                        @class = "editUserTypeFrm"
                    }))
                    {
                        <input type="text" name="SubClassID" value="" style="display:none;" />
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-append">
                                    <span class="input-group-text">
                                        上级类型
                                    </span>
                                </div>
                                <select class="custom-select" name="UserTypeID" id="UserType">
                                    <option selected>Choose...</option>
                                    @foreach (var item in userType)
                                    {
                                        <option value="@item.TypeID">@item.TypeName</option>
                                    }
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>群组名称</label>
                            <input type="text" name="SubClassName" value="" class="form-control" />
                        </div>
                        <div class="form-group">
                            <label>状态</label>
                            <div>
                                <input type="radio" name="state" value="Y" checked />
                                启用
                                <input type="radio" name="state" value="N" />
                                禁用
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                            <button class="btn btn-primary btn-sureEditUserType">修改</button>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    ;
    !function () {
        var $addUserTypeFrm = $('#SysUserTypeManager .addUserType');
        var $btnDelUserType = $('#SysUserTypeManager .btn-delUserType');
        var $btnSureDelUserType = $('#SysUserTypeManager .btn-sureDelUserType');
        var $btnEditUserType = $('#SysUserTypeManager .btn-editUserType');
        var $editUserTypeFrm = $('#SysUserTypeManager .editUserTypeFrm');

        var currentId = null;

        $addUserTypeFrm.submit(function (e) {
            e.preventDefault();

            $addUserTypeFrm.ajaxSubmit({
                type: 'POST',
                dataType: 'json',
                success: function (e) {
                    $tab.reloadTab('#SysUserTypeManager');
                }
            });
        });

        $btnDelUserType.on('click', function () {
            currentId = $(this).attr('data-id');
        });

        $btnEditUserType.on('click', function () {
            currentId = $(this).attr('data-id');

            $('.editUserTypeFrm input[name=SubClassID]').val(currentId);
        });

        $editUserTypeFrm.on('submit', function (e) {
            e.preventDefault();

            $editUserTypeFrm.ajaxSubmit({
                type: 'PUT',
                dataType: 'json',
                success: function (data) {
                    $('.modeal').modal('hide');
                    $tab.reloadTab('#SysUserTypeManager');
                }
            });
        });

        $btnSureDelUserType.on('click', function () {
            $.ajax({
                url: '/setting/SysUserTypeManager',
                dataType: 'json',
                type: 'DELETE',
                data: { id: currentId },
                success: function (data) {
                    if (data) {
                        $('.modal').modal('hide');
                        $tab.reloadTab('#SysUserTypeManager');
                    }
                }
            });
        });
    }();
</script>